package com.shfl.servermanage.logic.api;

import com.github.pagehelper.PageInfo;
import com.shfl.servermanage.common.*;
import com.shfl.servermanage.logic.constants.CommonConstants;
import com.shfl.servermanage.logic.model.request.AddSiteConfigRequest;
import com.shfl.servermanage.logic.model.response.ServerSitePageResponse;
import com.shfl.servermanage.logic.model.response.SiteConfigPageResponse;
import com.shfl.servermanage.logic.service.SysSiteConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/site/config")
@Api(value = "站点配置文件", description = "站点配置文件")
public class SysSiteConfigController {


    @Autowired
    private SysSiteConfigService sysSiteConfigService;

    @GetMapping(value = "/page/list")
    public ApiResultPagingHelper<SiteConfigPageResponse, Void> page(
            RequestHelper requestHelper
    ) {
        requestHelper.apply();
        Map<String, Object> objectMap = requestHelper.getKeyMap();
        PageInfo<SiteConfigPageResponse> pageInfo = sysSiteConfigService.findPage(requestHelper.getPageable(), objectMap);
        if (pageInfo.getTotal() > 0) {
            pageInfo.getList().forEach(item -> {
//                item.setJobStatusValue(CommonConstants.JobStatus.getName(item.getJobStatus()));
//                item.setJobTypeValue(CommonConstants.JobType.getName(item.getJobType()));
            });
        }
        return new ApiResultPagingHelper<>(SystemConstant.RE_SUCCESS_CODE,
                pageInfo.getTotal(), pageInfo.getPageSize(), pageInfo.getPageNum(),
                pageInfo.getList());
    }

    @GetMapping(value = "/list")
    public ApiResultListHelper<SiteConfigPageResponse> list(
            RequestHelper requestHelper
    ) {
        requestHelper.apply();
        Map<String, Object> objectMap = requestHelper.getKeyMap();
        objectMap.put("deleteFlg", CommonConstants.DeleteFlg.NOT_DELETE.getCode());
        List<SiteConfigPageResponse> list = sysSiteConfigService.findList(objectMap);
        return new ApiResultListHelper<>(SystemConstant.RE_SUCCESS_CODE, list);
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "新增", notes = "新增")
    public ApiResultHelper add(
            @RequestBody AddSiteConfigRequest addSiteConfigRequest
    ) throws Exception {
        sysSiteConfigService.add(addSiteConfigRequest);
        return new ApiResultHelper(SystemConstant.RE_SUCCESS_CODE);
    }
}
